<template>
    <div>
      <div class="title">
        <h3>我的地址</h3>
      </div>

      <div v-if="list.length > 0" class="moren addr">
        <h3><i>默认</i>收货人: {{defaultConsignee.name}} <span>{{defaultConsignee.phone}}</span></h3>
        <p>收货地址: {{defaultConsignee.address}}</p>
      </div>

      <div class="list">
        <ul>
          <li class="item" v-for="(item, index) in list" :key="item.id">
            <div class="info addr">
              <h3>收货人: {{item.name}} <span>{{item.phone}}</span></h3>
              <p>收货地址: {{item.address}}</p>
            </div>
            <button @click="onEdit(item)">编辑</button>
          </li>
        </ul>
      </div>
      <div class="add">
        <button @click="onEdit()">添加新地址</button>
      </div>
    </div>
</template>

<script>
import Consignee from '../api/order/consignee';
import LocalStorage from '../utils/local-storage';

export default {
  name: 'address',
  data() {
    return {
      list: []
    }
  },

  computed: {
    defaultConsignee() {
      return this.setDefault();
    },

  },

  mounted() {
    this.initList()
  },

  methods: {
    async initList() {
      const list = await Consignee.getList();
      console.log('list: ', list);
      this.list = list;
    },

    setDefault() {
      const {list} = this;
      let [c] = list.filter(el => el.default > 0);
      if (!c && list.length > 0) {
        c = list[0];
      }
      LocalStorage.setObject('def-consignee', c);
      return c;
    },

    onEdit(consignee=null) {
      LocalStorage.setObject('edit-consignee', consignee);
      LocalStorage.setObject('consignee-num', this.list.length);
      this.$router.push('add-address');
    },

  },

}
</script>

<style scoped>
.title{
  height: 1.4rem;
  -webkit-box-shadow: 0 1px 3px #cddcdc;
  box-shadow: 0 1px 3px #cddcdc;
}
.title h3{
  height: 1.4rem;
  line-height: 1.4rem;
  text-align: center;
  color: #000;
  font-size: .42rem;
}
.moren{
  padding: .3rem;
  border-bottom: .2rem solid #f1f1f1;
}
.addr h3{
  font-size: .4rem;
  color: #333;
  height: .6rem;
  line-height: .6rem;
}
.addr h3 i{
  color: #fff;
  font-size: .32rem;
  background: #f00;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  margin-right: 4px;
  padding: 3px 5px;
}
.addr h3 span{
  float: right;
}
.addr p{
  font-size: .34rem;
  color: #555;
  margin-top: .1rem;
}
.item{
  display: flex;
  display: -webkit-flex;
  justify-content: space-between;
  padding: .2rem .3rem;
  border-bottom: 1px solid #e1e1e1;
  align-items: center;
}
.item .info{
  width: 8rem;
}
.item button{
  background: #fff;
  border: none;
  color: #888;
  border-left: 1px solid #d1d1d1;
  line-height: .6rem;
  height: .6rem;
  padding-left: .2rem;
}
.add{
  margin: .8rem auto;
  width: 5rem;
}
.add button{
  display: block;
  width: 100%;
  height: 1rem;
  line-height: 1rem;
  background: #f00;
  color: #fff;
  text-align: center;
  font-size: .38rem;
  border: none;
}
</style>
